<template>
  <el-dialog 
    v-model="visible" 
    title="应用管理" 
    width="1000px" 
    top="5vh"
    :lock-scroll="true"
    append-to-body
  >
    <div class="app-manage-container">
      <!-- 左侧：可用应用列表 -->
      <div class="available-apps">
        <div class="section-header">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索应用..."
            prefix-icon="Search"
            clearable
            size="small"
          />
        </div>
        <div class="apps-grid">
          <div
            v-for="app in filteredApps"
            :key="app.id"
            class="app-card"
            :class="{ added: isOnDesktop(app.id) }"
          >
            <div class="app-card-icon" :style="{ background: app.color }">
              <el-icon :size="32">
                <component :is="app.icon" />
              </el-icon>
            </div>
            <div class="app-card-info">
              <div class="app-card-name">{{ app.name }}</div>
              <div class="app-card-desc">{{ app.description }}</div>
            </div>
            <el-button
              v-if="!isOnDesktop(app.id)"
              type="primary"
              size="small"
              @click="$emit('add-app', app)"
            >
              添加
            </el-button>
            <el-button
              v-else
              type="info"
              size="small"
              disabled
            >
              已添加
            </el-button>
          </div>
        </div>
      </div>

      <!-- 右侧：桌面图标列表 -->
      <div class="desktop-apps">
        <div class="section-header">
          <h3>桌面图标 ({{ desktopIcons.length }})</h3>
          <el-button type="danger" size="small" @click="$emit('clear-desktop')">清空桌面</el-button>
        </div>
        <div class="desktop-apps-list">
          <div
            v-for="(icon, index) in desktopIcons"
            :key="icon.id"
            class="desktop-app-item"
          >
            <div class="app-item-icon" :style="{ background: icon.color }">
              <el-icon :size="24">
                <component :is="icon.icon" />
              </el-icon>
            </div>
            <div class="app-item-info">
              <div class="app-item-name">{{ icon.label }}</div>
              <div class="app-item-path">{{ icon.path }}</div>
            </div>
            <div class="app-item-actions">
              <el-button
                type="primary"
                size="small"
                circle
                @click="$emit('open-app', icon)"
              >
                <el-icon><FolderOpened /></el-icon>
              </el-button>
              <el-button
                type="danger"
                size="small"
                circle
                @click="$emit('remove-app', index)"
              >
                <el-icon><Delete /></el-icon>
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { FolderOpened, Delete } from '@element-plus/icons-vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  availableApps: {
    type: Array,
    required: true
  },
  desktopIcons: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'add-app', 'remove-app', 'open-app', 'clear-desktop'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const searchKeyword = ref('')

const filteredApps = computed(() => {
  if (!searchKeyword.value) {
    return props.availableApps
  }
  const keyword = searchKeyword.value.toLowerCase()
  return props.availableApps.filter(app => 
    app.name.toLowerCase().includes(keyword) || 
    app.description.toLowerCase().includes(keyword)
  )
})

const isOnDesktop = (appId) => {
  return props.desktopIcons.some(icon => icon.id === appId)
}
</script>

<style scoped>
.app-manage-container {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 24px;
  height: 70vh;
  max-height: 700px;
  min-height: 500px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.section-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

/* 可用应用列表 */
.available-apps {
  border-right: 1px solid #e4e7ed;
  padding-right: 24px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.apps-grid {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  align-content: start;
}

.app-card {
  padding: 16px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.app-card:hover {
  border-color: #409eff;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.15);
  transform: translateY(-2px);
}

.app-card.added {
  background: #f0f9ff;
  border-color: #b3d8ff;
}

.app-card-icon {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.app-card-info {
  text-align: center;
  width: 100%;
}

.app-card-name {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.app-card-desc {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

/* 桌面应用列表 */
.desktop-apps {
  padding-left: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.desktop-apps-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-content: start;
}

.desktop-app-item {
  padding: 12px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.2s;
}

.desktop-app-item:hover {
  background: #f5f7fa;
  border-color: #c0c4cc;
}

.app-item-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

.app-item-info {
  flex: 1;
  min-width: 0;
}

.app-item-name {
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 4px;
}

.app-item-path {
  font-size: 12px;
  color: #909399;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-item-actions {
  display: flex;
  gap: 8px;
}

/* 滚动条美化 */
.apps-grid::-webkit-scrollbar,
.desktop-apps-list::-webkit-scrollbar {
  width: 6px;
}

.apps-grid::-webkit-scrollbar-thumb,
.desktop-apps-list::-webkit-scrollbar-thumb {
  background: #dcdfe6;
  border-radius: 3px;
}

.apps-grid::-webkit-scrollbar-thumb:hover,
.desktop-apps-list::-webkit-scrollbar-thumb:hover {
  background: #c0c4cc;
}
</style>

